cmake_minimum_required(VERSION 2.8)
project(grpc_server)

add_compile_options(-std=c++11)
add_compile_options(-pthread)

include_directories(
    ${CMAKE_CURRENT_LIST_DIR}/../${PROJECT_NAME}/include  
)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")

find_package(Protobuf REQUIRED)
find_package(GRPC REQUIRED)

file(GLOB_RECURSE test_files test/*.cpp)

set(PROTOS
    ${CMAKE_CURRENT_SOURCE_DIR}/proto/sukibean.proto
)

set(PROTO_SRC_DIR ${CMAKE_CURRENT_BINARY_DIR}/proto-src)
file(MAKE_DIRECTORY ${PROTO_SRC_DIR})

protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS ${PROTO_SRC_DIR} ${PROTOS})
grpc_generate_cpp(GRPC_SRCS GRPC_HDRS ${PROTO_SRC_DIR} ${PROTOS})

add_library(${PROJECT_NAME} SHARED ${PROTO_SRCS} ${GRPC_SRCS})

target_link_libraries(${PROJECT_NAME} 
${PROTOBUF_LIBRARIES} 
        gRPC::grpc++_reflection
        protobuf::libprotobuf
)

add_custom_command(TARGET ${PROJECT_NAME} PRE_BUILD
                   COMMAND cp ${PROTO_SRC_DIR}/*.h ${CMAKE_CURRENT_SOURCE_DIR}/include)

option(Test "Set to switch to build use Test"  ON)


if(Test)
file(GLOB_RECURSE test_files test/*.cpp)
add_executable(${PROJECT_NAME}_test
  ${test_files}
)      
target_link_libraries(${PROJECT_NAME}_test
        ${PROJECT_NAME}
)
endif()

install(TARGETS ${PROJECT_NAME}
        DESTINATION lib
        )
install(DIRECTORY include/
        DESTINATION include)